//
//  MBProgressHUD+Http.m
//  TaskManager
//
//  Created by Sun on 2019/3/11.
//  Copyright © 2019 Sun. All rights reserved.
//

#import "MBProgressHUD+Http.h"

#define kDetailsLabelFontSize 16.f

@implementation MBProgressHUD (Http)

+ (void)leShowHUDWithMessage:(NSString *)message activity:(BOOL)activity {
    if (!message || message.length == 0) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:MAIN_WINDOW animated:YES];
    [MAIN_WINDOW bringSubviewToFront:hud];
    if (!activity) {
        hud.mode = MBProgressHUDModeText;
    }
    hud.userInteractionEnabled = NO;
    //hud.labelText = message;
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize];
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:20];
}

+ (void)leShowSuccessHUDAddedTo:(UIView *)view message:(NSString *)message {
    if (!message || message.length == 0) {
        return;
    }
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize];
    // Set custom view mode
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.backgroundColor = HexColorA(0x000000, 0.6);
    hud.contentColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:1.5];
}

+ (void)leShowSuccessHUDAddedTo:(UIView *)view message:(NSString *)message delayOperation:(void(^)(void))operation {
    if (!message || message.length == 0) {
        return;
    }
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize];
    // Set custom view mode
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.backgroundColor = HexColorA(0x000000, 0.6);
    hud.contentColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:0.5];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        operation();
    });
}

+ (void)leShowFailedHUD:(UIView *)view message:(NSString *)message {
    if ([FactoryMethod isNull:message] || !message || message.length == 0) {
        return;
    }
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    //    hud.labelFont = [UIFont systemFontOfSize:12];
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:kDetailsLabelFontSize];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = HexColorA(0x000000, 0.6);
    hud.contentColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:1.5];
}

+ (void)hidenHUD {
    MBProgressHUD *HUD = [MBProgressHUD HUDForView:MAIN_WINDOW];
    [HUD hideAnimated:YES];
    [MBProgressHUD hideAllHUDsForView:MAIN_WINDOW animated:YES];
}

@end
